import { forEachFormOptions } from '@/utils/formMakeUtils.js';

/**
 * 获取公式数据
 * @param {Object} data 公式节点的options
 * @param {Object} selectWidget 当前的公式节点
 * @param {Array} keyToFormula keyToFormula
 * @returns
 */
export const getFormulaVariable = (data, selectWidget, keyToFormula) => {
  let tagFormula = '';
  // 是否复杂公式，复杂公式通过接口计算，简单公式直接计算
  /** 复杂判定
     * 1、有筛选条件
     * 2、有引用公式字段
     */
  const oldFormulaOrigin = selectWidget ? selectWidget.options.formulaOrigin || [] : null;
  const keys = [];
  if (data.formulaOrigin && data.formulaOrigin.length > 0) {
    data.formulaOrigin.forEach(j => {
      if (j.type !== 'math' && j.type !== 'number') {
        keys.push(j.key);
        if (keyToFormula) quoteAddTag(j, keyToFormula, selectWidget);
      }
      tagFormula = setTagFormula(j, tagFormula);
    });
  }
  if (oldFormulaOrigin && keyToFormula) {
    oldFormulaOrigin.forEach((i) => {
      if (i.type !== 'math' && i.type !== 'number' && !keys.includes(i.key)) {
        quoteRemoveTag(i, keyToFormula, selectWidget);
      }
    });
  }

  return {
    formulaOrigin: data.formulaOrigin,
    tagFormula
  };
};
export const setTagFormula = (item, tagFormula) => {
  if (item.type !== 'math' && item.type !== 'number') {
    tagFormula = `${tagFormula}#${item.isLinkField ? item.linkFormKey + '.' || '' : ''}${item.key}#`;
  } else {
    tagFormula = tagFormula + item.key;
  }
  return tagFormula;
};
// 移除动态字段事件
export function quoteRemoveTag(el, keyToFormula, selectWidget) {
  let key = el.key;
  if (el.linkFormKey && el.isLinkField) {
    key = `linkForm*${el.linkFormKey}*${key}`;
  }
  if (el.linkFormKey && el.isSubTable) {
    key = `subform*${el.linkFormKey}*${key}`;
  }
  if (keyToFormula[key]) {
    keyToFormula[key] = keyToFormula[key].filter(i => {
      return i !== selectWidget.key;
    });
  }
  if (keyToFormula[key] && keyToFormula[key].length === 0) {
    delete keyToFormula[key];
  }
}
export function quoteAddTag(el, keyToFormula, selectWidget) {
  let key = el.key;
  if (el.linkFormKey && el.isLinkField) {
    key = `linkForm*${el.linkFormKey}*${key}`;
  }
  if (el.linkFormKey && el.isSubTable) {
    key = `subform*${el.linkFormKey}*${key}`;
  }
  if (!keyToFormula[key]) {
    // $set(keyToFormula, key, []);
    keyToFormula[key] = [];
  }
  const isRepeat = keyToFormula[key].some(i => i === selectWidget.key);
  if (!isRepeat) {
    keyToFormula[key].push(selectWidget.key);
  }
}
/**
 * 如果被引用的字段被改变，如：修改名称，删除字段
 * @param {String} type changeName：修改名称 remove:删除字段
 * @param {Object} formOptions formOptions
 * @param {Object} keyToFormula keyToFormula
 * @param {Object} targetElement 修改或被删除的字段
 * @param {String} linkFormKey 关联表单里面的字段被引用的话，它在 keyToFormula 的格式是 `${标识}*${关联表单的key}*${目标字段key}` 标识为 linkForm 或 subform
 */
export function formulaChange(type, formOptions, targetElement, linkFormKey) {
  const keyToFormula = formOptions && formOptions.config && formOptions.config.keyToFormula;
  const formOptionsList = formOptions && formOptions.list;
  let targetKey = targetElement.key;
  if (linkFormKey) {
    targetKey = linkFormKey;
  } else if (targetElement.isSubTable && targetElement.linkFormKey) {
    targetKey = `subform*${targetElement.linkFormKey}*${targetElement.key}`;
  }
  if (!targetElement || !keyToFormula || !keyToFormula[targetKey]) return;
  const formulaListKeys = keyToFormula && keyToFormula[targetKey]; // 涉及到的公式字段

  forEachFormOptions(formOptionsList, (element) => {
    if (formulaListKeys.includes(element.key)) {
      if (type === 'changeName') {
        element.options.formulaOrigin.forEach(varItem => {
          if (varItem.key === targetElement.key) {
            varItem.name = targetElement.name;
          }
        });
      } else if (type === 'remove') {
        let tagFormula = '';
        let targetElementKey = targetElement.key;

        if (linkFormKey || targetElement.isSubTable) {
          targetElementKey = targetKey.split('*')[2] || undefined;
        }
        element.options.formulaOrigin = element.options.formulaOrigin.filter(varItem => {
          const hasKey = varItem.key !== targetElementKey;
          if (hasKey) {
            tagFormula = setTagFormula(varItem, tagFormula);
          }
          return hasKey;
        });
        element.options.tagFormula = tagFormula;
        delete keyToFormula[targetKey];
      }
    }
  });
}
